package com.huayi.test.springbootcomm.controller;

import com.alibaba.fastjson.JSONObject;
import com.huayi.test.springbootcomm.component.ApplicationContextProvider;
import com.huayi.test.springbootcomm.entity.TblUser;
import com.huayi.test.springbootcomm.service.IserviceContext;
import com.huayi.test.springbootcomm.service.IserviceFactory;
import com.huayi.test.springbootcomm.service.UserService;
import com.huayi.test.springbootcomm.service.impl.ServiceContext;
import com.huayi.test.springbootcomm.trans.service.IService;
import com.huayi.test.springbootcomm.trans.service.Test40102;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 入口类:
 *
 * {
 *         "requestData":{
 *             "systemHeader":{
 *                 "sourceJnno":"123021031200",
 *                 "tranId":"",
 *                 "tranName":"",
 *                 "channelNo":""
 *             },
 *             "body":{
 *                 "id": "1243",
 *                 "userName":"ljj",
 *                 "userId":"2001",
 *                 "token":"123sadfsadf",
 *                 "tranDt":"20210107",
 *                 "tranTime":""
 *             }
 *         }
 * }
 */
@RestController
@RequestMapping("/user")
public class EntranceController {
    private static final Logger LOG = LoggerFactory.getLogger(EntranceController.class);

    private final UserService userService;

    private final ApplicationContextProvider applicationContextProvider;

    @Autowired
    private IserviceFactory serviceFactory;
    /**
     * 基于构造器注入UserService
     */
    @Autowired
    public EntranceController(UserService userService, ApplicationContextProvider applicationContextProvider) {
        this.userService = userService;
        this.applicationContextProvider = applicationContextProvider;
    }

    @RequestMapping(value = "/entrance",method = RequestMethod.POST)
    public @ResponseBody String call(@RequestBody String json){
        LOG.info("entrance>>>>>>>>>>>>>>>>>>>>>>");
        LOG.info("request:"+json);
        JSONObject reqJson = JSONObject.parseObject(json);
        JSONObject requestData = JSONObject.parseObject(reqJson.getString("requestData"));
        JSONObject body = JSONObject.parseObject(requestData.getString("body"));
        String tranCode = body.getString("tranCode");
        LOG.info("交易码:[{}]",tranCode);
        IserviceContext context = new ServiceContext();
        Map<String,Object> reqMap = new HashMap<>();
        Map<String,Object> respMap = new HashMap<>();
        context.setReqMap(reqMap);
        context.setRespMap(respMap);
        reqMap.put("reqJson",json);
        reqMap.put("body",body.toJSONString());
        // 执行目标方法
        IService service = serviceFactory.getService(tranCode);
        // 交易层已经处理了异常并放入context的respMap里面
        service.excute(context);

        return returnJson(context.getRespMap());

    }


    /***
     * 返回响应码
     * @param body
     * @return
     */
    private String returnJson(Map<String ,Object> body){
        Map<String,Object> responseData = new HashMap<>();
        Map<String ,Object> systemHeader = new HashMap<>();
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("responseData",responseData);
        responseData.put("systemHeader",systemHeader);
        responseData.put("body",body);
        responseData.put("code","200");
        responseData.put("msg","响应成功");
        JSONObject json = new JSONObject(jsonMap);
        return json.toJSONString();
    }



}
